/**
 * @Description: TODO(用一句话描述该文件做什么)
 */
package org.shanktech.tshare.binlogsync.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * @author sztan nanjing_xiaotan@163.com
 * @date 2017年6月24日 下午8:44:58
 * @version V1.0
 */

@Configuration
@MapperScan(basePackages = MybatisVersion1Config.PACKAGE, sqlSessionFactoryRef = "version1SqlSessionFactoryBean")
public class MybatisVersion1Config {
  public final static String PACKAGE =
      "org.shanktech.tshare.binlogsync.dao.version1";

  @Bean(name = "version1DataSource")
  @ConfigurationProperties(prefix = "spring.datasource.version1")
  public DataSource dataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean(name = "version1SqlSessionFactoryBean")
  public SqlSessionFactory sqlSessionFactory(
      @Qualifier("version1DataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    factoryBean.setDataSource(dataSource);
    ResourcePatternResolver resolver =
        new PathMatchingResourcePatternResolver();
    try {
      factoryBean.setMapperLocations(
          resolver.getResources("classpath:mybatis/sqlmapper/version1/*.xml"));
      return factoryBean.getObject();
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
  }

  @Bean(name = "version1SqlSessionTemplate")
  public SqlSessionTemplate sqlSessionTemplate(
      @Qualifier("version1SqlSessionFactoryBean") SqlSessionFactory sqlSessionFactory)
      throws Exception {
    SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory);
    return template;
  }

  @Bean(name = "version1PlatformTransactionManager")
  @ConditionalOnMissingBean
  public PlatformTransactionManager annotationDrivenTransactionManager(
      @Qualifier("version1DataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }

  @Bean(name = "version1JdbcTemplate")
  public JdbcTemplate primaryJdbcTemplate(
      @Qualifier("version1DataSource") DataSource dataSource) {
    return new JdbcTemplate(dataSource);
  }
}
